<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class uindex extends CI_Controller {
    var $theme_data = array(
        'layout_main' => 'frontend/layout',
        'left' => 'frontend/include/left',
        'right' => 'frontend/include/right',
    );	
    function __construct() {
        parent::__construct();
        $this->load->model("scategory");
        $this->load->model("muser");
        $this->load->model("msoft");
        $this->load->model("msoft_tag");
        $this->load->model("mtag");
        $this->load->model("mcategory");
        $this->load->model("mlinkdown");
        $this->load->model("mlogdownload");
        $this->load->model("mmanufacturer");
		$this->scache->html();
    }
    public function index() {
        $data = array();
		$cat_id = 1;
		$data = $this->scache->script('msoft.category.getCategory','$this->msoft->getCategory();');
		$data['crumbs']  = $this->scategory->get_crumbs($cat_id);
		
		$data['hots'] = $this->scache->script('msoft_getsNewInsert','$this->msoft->getsNewInsert(0,'.$this->config->item('POSTS_PER_PAGE_HOT_INDEX').');');
		$data['softs'] = $this->scache->script('msoft_getsNewUpdate','$this->msoft->getsNewUpdate(0,'.$this->config->item('POSTS_PER_PAGE_INDEX').');');
				$data['position'] = 'trang-chu';
		$data["seo"] = $this->seo->get($data['position'], 0);
        $this->theme_data["main"] = "frontend/uindex/home";
        $this->template->view($this->theme_data, $data);
    }
	
	public function category($cateCode = '', $page = 0) {
        $data = array();
        $category = $this->mcategory->get($cateCode, 'CAT_CODE');
		if (!$category)
           redirect(page404(), 301);
		$crumbs = $this->scategory->get_crumbs($category->CAT_ID);
		$arr_catemobile = array(249,250,251,252,253);
		$this->db->order_by("CAT_ORDER desc");
		$this->db->where_not_in('CAT_ID',$arr_catemobile);
		$data['categoryParent'] = $this->mcategory->gets(array('CAT_PARENT'=>0));
		if ($data['categoryParent']) {
			for($i=0; $i<count($data['categoryParent']); $i++) {
				$cate = $data['categoryParent'][$i];
				if($cate->CAT_ID == 3){
					foreach($arr_catemobile as $r){
						$data['categoryParent'][$i]->CHILD[] =  $this->scategory->get_cate_info($r);
					}
				}
				else {
				$data['categoryParent'][$i]->CHILD =  $this->scategory->get_cat_format_tree($data['categoryParent'][$i]->CAT_ID);
				}
			}
		}
		$data['categorySub'] = $this->scategory->get_cat_format_tree($crumbs[0]['CAT_ID']);
		$data['crumbs'] = $crumbs;
		if($data['categorySub']){
			for ($i=0; $i<count($data['categorySub']); $i++){
				 $sub = $this->scategory->get_cat_format_tree($data['categorySub'][$i]['CAT_ID']);
				 $data['categorySub'][$i]['SUB_DATA'] = array();
				 // if ($category->CAT_PARENT == $crumbs[0]['CAT_ID'])
				 // $arr_CateID[] = $data['categorySub'][$i]['CAT_ID'];
				 if($sub){
					$data['categorySub'][$i]['SUB_DATA'] = $sub;
					if($category->CAT_PARENT == 0){
					 foreach($sub as $rsub){
						// Dang o menu cap 3 thi khong lay DL tu cate nay.
						if (count($crumbs) <3)
						 $arr_CateID[] = $rsub['CAT_ID'];
					 }
					}
				 }
			}
		}
		$arr_CateID[] = $category->CAT_ID;
		$sub_cate = $this->scategory->get_cat_format_tree($category->CAT_ID);
		if ($sub_cate) foreach ($sub_cate as $c)  {
			$arr_CateID[] = $c['CAT_ID'];
		}
        $page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = $this->config->item('POSTS_PER_PAGE_CATEGORY');
        $config['cur_page'] = $page;
        $config['base_url'] = $this->template->url_page . $cateCode;
        $config['first_url'] = $config['base_url'] . ".html";
		$config['total_rows'] = $this->msoft->num(1, 'SOFT_STATUS');
        $this->pagination->initialize($config);
        $this->db->limit($config['per_page'], ($page - 1 ) * $config['per_page']);
        $data["total"] = $config['total_rows'];
		$data["softs"] = $this->msoft->getsNewUpdate($arr_CateID, $config['per_page']);
		$category->SEO_METATITLE = $category->CAT_NAME;
        $data["category"] = $category;				$data['position'] = 'category';		$data["seo"] = $this->seo->set($category, $page);
		$this->theme_data["main"] = "frontend/uindex/category";
        $this->template->view($this->theme_data, $data);
    }
	public function view($softCode = '') {
		$data = array();
		if($softCode =='')
			redirect(page404(), 301);
		$softID = explode("-", $softCode);
        $softID = end($softID);
        if (!$softID || $softID <= 0)
            redirect(page404(), 301);
		$data = $data = $this->scache->script('msoft.category.getCategory','$this->msoft->getCategory();');
		$data['soft'] = $this->msoft->getSoft($softID, 'SOFT_ID');
		if (!$data['soft'])
            redirect(page404(), 301);
		$this->seo->set_title($data['soft']->SOFT_NAME);
		$this->seo->set_h1($data['soft']->SOFT_NAME);
		$this->seo->set_description($data['soft']->SOFT_CONTENT_SHORT);
		$data["seo"] = $this->seo->result();
		
		$this->msoft->updateView($softID);
		$data["breadcrums"] = $this->scategory->get_crumbs($data['soft']->CAT_ID);
		
		/*
		$data["related"] = $this->msoft->getRelated($data['soft']->CAT_ID,$softID,10);
		if(!$data["related"]){
			$data["related"] = $this->msoft->getRelated($data['soft']->CAT_ID,$softID,10,2);
		}
		*/
		$arr_tagsID = array();
		$data["tags"] = array();
		
		$tagsID = $this->msoft_tag->gets($softID,'SOFT_ID');
		foreach($tagsID as $tag){
			$arr_tagsID[] = $tag->TAG_ID;
		}
		if(count($arr_tagsID)>0)
			$data["tags"] = $this->mtag->where_in($arr_tagsID,'TAG_ID');		$data['position'] = 'detail';
		$this->theme_data["left"] = null;
		$this->theme_data["main"] = "frontend/uindex/view";
        $this->template->view($this->theme_data, $data);
	}
	function download($softCode = '') {
		$data = array();
		$data = $this->scache->script('msoft.category.getCategory','$this->msoft->getCategory();');
		if($softCode =='')
			  redirect(page404(), 301);			  
		$softID = explode("-", $softCode);
        $softID = end($softID);
        if (!$softID || $softID <= 0)
            redirect(page404(), 301);
		$data['soft'] = $this->msoft->getSoft($softID, 'SOFT_ID');
		if (!$data['soft'])
            redirect(page404(), 301);
		$data["breadcrums"] = $this->scategory->get_crumbs($data['soft']->CAT_ID);
		$data['downloads'] = $this->mlinkdown->gets($softID, 'SOFT_ID');
		$data['soft']->SEO_METATITLE = 'Download '.$data['soft']->SEO_METATITLE;
		$this->msoft->updateDownload($softID);
		$data["seo"] = $this->seo->set($data['soft'], 0);
		/*
		$data["related"] = $this->msoft->getRelated($data['soft']->CAT_ID,$softID,10);
		if(!$data["related"]){
			$data["related"] = $this->msoft->getRelated($data['soft']->CAT_ID,$softID,10,2);
		}
		*/		$data['position'] = 'download';
		$this->theme_data["left"] = null;
		$this->theme_data["main"] = "frontend/view/download";
        $this->template->view($this->theme_data, $data);
	}
	function publisher($pubCode = '', $page = 0) {
		$data = array();
		$cat_id = 1;
		$data = $this->scache->script('msoft.category.getCategory','$this->msoft->getCategory();');
		$data['orderBy'] = @$this->input->get('soft',TRUE);
		$data["man"] = $this->mmanufacturer->get($pubCode ,'MAN_CODE');
		if (!$data['man'])
            redirect(page404(), 301);
		$page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = $this->config->item('POSTS_PER_PAGE_PUBLISHER');
        $config['cur_page'] = $page;
        $config['base_url'] = $this->template->url_page .'publisher/'. $pubCode;
        $config['first_url'] = $config['base_url'] . ".html";
		$this->db->where('MAN_ID', $data['man']->MAN_ID);
        $config['total_rows'] = $this->msoft->num(1, 'SOFT_STATUS');
        $this->pagination->initialize($config);
        $this->db->limit($config['per_page'], ($page - 1 ) * $config['per_page']);
        $data["total"] = $config['total_rows'];
		$data['softs'] = $this->msoft->getsNewMan($data["man"]->MAN_ID, @$data['orderBy']);
		$data["seo"] = $this->seo->set($data["man"], $page);		$data['position'] = 'publisher';
		$this->theme_data["left"] = null;
        $this->theme_data["main"] = "frontend/uindex/publisher";
        $this->template->view($this->theme_data, $data);
	}
	function redirectdownload(){
		$post = $this->input->post();
		if($post && @$post['downloadurl'] != ''){
			$link = @$post['downloadurl'];
			if(!$link && $link =='')
				die("");				  
			$data['link'] = $this->mlinkdown->get($link, 'LINK_ID');
			if (!$data['link'])
				die("");
			if($data['link']->LINK_DOWNCOUNT == '' || $data['link']->LINK_DOWNCOUNT <0){
				$data['link']->LINK_DOWNCOUNT = 0;
			}			
			$arr_insert =array();
			$arr_insert['SOFT_ID'] = $data['link']->SOFT_ID;
			$arr_insert['LINK_ID'] = $data['link']->LINK_ID;
			$arr_insert['LOGDOWN_TIME'] = time();
			$arr_insert['LOGDOWN_IP'] = $this->input->ip_address();
			$this->mlogdownload->insert($arr_insert);
			$this->mlinkdown->update($data['link']->LINK_ID,'LINK_ID', array('LINK_DOWNCOUNT'=>$data['link']->LINK_DOWNCOUNT+1));
		}	}
	function search($keyword = '', $page = 0){
		$data = array();
		$cat_id = 1;
		$keyword1 = @$this->input->get('q',TRUE);
		$Skeyword = trim(urldecode($keyword));
		
		$manCode = @$this->input->get('platformId',TRUE);
		if(isset($keyword1) && $keyword1 != ''){
			$urlRedirect = base_url()."search/".$keyword1.".html";
			if(isset($manCode) && $manCode != ''){
				$urlRedirect.="?platformId=".$manCode;
			}
			redirect($urlRedirect, 301);
		}
		$data = $this->scache->script('msoft.category.getCategory','$this->msoft->getCategory();');
		$data['orderBy'] = @$this->input->get('soft',TRUE);
		$data['keyword'] = $keyword;
		$data['manCode'] = $manCode;
		$man = $this->mmanufacturer->get($manCode,'MAN_CODE');
		$manID = 0;
		if(isset($manCode) && $manCode != ''){
			if($man)
				$manID = $man->MAN_ID;
		}
		$page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = $this->config->item('POSTS_PER_PAGE_SEARCH');
        $config['cur_page'] = $page;
		$config['base_url'] = $this->template->url_page .'search/'. $keyword;
        $config['first_url'] = $config['base_url'] . ".html";
		if(isset($manCode) && $manCode != ''){
			$config['first_url'].= "?platformId=".$manCode;
			$config['suffix']= ".html?platformId=".$manCode;
		}
        $config['total_rows'] = $this->msoft->getsSearch($Skeyword, $manID,'','num');
        $this->pagination->initialize($config);
        
        $data["total"] = $config['total_rows'];		$this->db->limit($config['per_page'], ($page - 1 ) * $config['per_page']);
		$data['softs'] = $this->msoft->getsSearch($Skeyword,$manID, @$data['orderBy'],'gets');
		$this->seo->set_title("Download ".$Skeyword);
        $this->seo->set_h1("Download ".$Skeyword);
        $this->seo->set_description("Download ".$Skeyword);
        $this->seo->set_keyword("Download ".$Skeyword);
        $data["seo"] = $this->seo->result();		$data['position'] = 'search';
		$this->theme_data["left"] = null;
        $this->theme_data["main"] = "frontend/uindex/search";
        $this->template->view($this->theme_data, $data);
	}
	function page404(){
		$data = array();
		$data = $this->scache->script('msoft.category.getCategory','$this->msoft->getCategory();');
		$this->output->set_status_header('404');
		set_status_header(404);				$data['position'] = 'page404';
		$this->theme_data["left"] = null;
		$this->theme_data["right"] = null;
		$this->theme_data["main"] = "frontend/include/page404";
        $this->template->view($this->theme_data, $data);
	}
}
